<?php
class cls_control_order extends cls_control_base
{
	function show()
	{
		$product_list_with_num = $_SESSION['cart'];
		if (empty($product_list_with_num))
		{
			header("location:../shoppingcart.php");
		}
		
		$serve = parent::get_serve();
		$product_info = $serve->get_cart_info($product_list_with_num);
		$total_price = $serve->get_total_price($product_list_with_num, $product_info);
		
		$this->smarty->assign('root_url', ROOT_URL);
		$this->smarty->assign('product_info', $product_info);
		$this->smarty->assign('total_price', $total_price); 
		$this->smarty->display('order/order.htm');
	}
	
	function get_cart_status()
	{
		$serve = parent::get_serve();
		$product_list_with_num = $_SESSION['cart'];
		$product_info = $serve->get_cart_info($product_list_with_num);
		$result = $serve->check_cart_valid($product_list_with_num, $product_info);
		echo json_encode($result);
	}
	
	function place_order()
	{
		if (empty($_SESSION['cart'])) {
			echo json_encode(array('success'=>false, 'msg'=>array(array('type'=>'Empty Cart'))));
			return;
		}
		if (empty($_SESSION['user'])) {
			echo json_encode(array('success'=>false, 'msg'=>array(array('type'=>'Not Login'))));
			return;
		}
		$product_list = $_SESSION['cart'];
		$user_info = array(
				'user_id' => $_SESSION['user']['ID'],
				'address' => $_REQUEST['address'],
			);
		
		$order_serve = parent::get_serve();
		$result = $order_serve->place_order($user_info, $product_list);
		
		if ($result['success']) {
			unset($_SESSION['cart']);
		}
		echo json_encode($result);
	}
	
	function get_order_list()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ORDER_INFO');
		global $smarty;
		$this->smarty->display('order/order_list.htm');
	}
	
	
	function order_list_show()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ORDER_INFO');
		$page = $_POST['page'];
		$rp = $_POST['rp'];
		$sortname = $_POST['sortname'];
		$sortorder = $_POST['sortorder'];
		
		if (!$sortname) $sortname = 'name';
		if (!$sortorder) $sortorder = 'desc';
		if($_POST['query']!=''){
			$where = "WHERE ".$_POST['qtype']." LIKE '%".$_POST['query']."%' ";
		} else {
			$where ='';
		}
		if($_POST['letter_pressed']!=''){
			$where = "WHERE `".$_POST['qtype']."` LIKE '".$_POST['letter_pressed']."%' ";
		}
		if($_POST['letter_pressed']=='#'){
			$where = "WHERE `".$_POST['qtype']."` REGEXP '[[:digit:]]' ";
		}
		$sort = "ORDER BY $sortname $sortorder";
		
		
		$order_serve = parent::get_serve();
		$order_list  = $order_serve->get_order_list($page,$rp,$where,$sort);
		$total = $order_serve->getCount($where);
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		
		header("Content-type: text/x-json");
		$json = "";
		$json .= "{\n";
		$json .= "page: $page,\n";
		$json .= "total: $total,\n";
		$json .= "rows: [";
		$rc = false;
		$i = 0;
		while($i<count($order_list) )
		{
			if ($rc) $json .= ",";
			$json .= "\n{";
			$json .= "id:'".$order_list[$i]['ID']."',";
			$json .= "cell:['".$order_list[$i]['ID']."','".$order_list[$i]['CustomerID']."'";
			$json .= ",'".addslashes($order_list[$i]['OrderTime'])."'";
			$json .= ",'".addslashes($order_list[$i]['TotalPrice'])."'";
			$json .= ",'".addslashes($order_list[$i]['Receiver'])."'";
			$json .= ",'".addslashes($order_list[$i]['Phone'])."'";
			$json .= ",'".addslashes($order_list[$i]['Postal'])."'";
			$json .= ",'".addslashes($order_list[$i]['Address'])."'";
			$json .= ",'".addslashes($order_list[$i]['OrderState'])."']";
		
			$json .= "}";
			$rc = true;
			$i++;
		}
		$json .= "]\n";
		$json .= "}";
		
		
		echo $json;
		
	}
	
	function get_orderItem()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ORDER_INFO');
		$pid = $_POST['items'];
		$orderItem_serve = parent::get_serve();
		$orderItem_list = $orderItem_serve->get_orderItem($pid); 
		
		echo json_encode($orderItem_list);
	}
	
	function modify_order()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ORDER_STATE');
		$items = rtrim($_POST['items'],",");
		$order_serve = parent::get_serve();
		$order_serve->modify_order($items);
		
		echo json_encode(null);
	}
	
	function export_order()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('ORDER_INFO');
		$excel = new PHPExcel();
		$order_serve = parent::get_serve();
		$order_list = $order_serve->get_order_list(1,1000000,"","");
		$excel->setActiveSheetIndex(0);
		$objActSheet = $excel->getActiveSheet();
		$objActSheet->setTitle('订单列表');
		$objActSheet->setCellValueExplicit('A1', '订单ID', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('B1', '顾客ID', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('C1', '下单时间', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('D1', '总价', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('E1', '订单状态', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('F1', '收货人', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('G1', '联系电话', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('H1', '邮编', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('I1', '地址', PHPExcel_Cell_DataType::TYPE_STRING);
		$row = 1;
		foreach ($order_list as $order)
		{
			$row++;
			$col = 65;
			foreach ($order as $key => $item)
			{
				if ($key == "PaymentTypeID")
					continue;
				$c = chr($col);
				$objActSheet->setCellValueExplicit("{$c}{$row}", $item, PHPExcel_Cell_DataType::TYPE_STRING);
				$col++;
			}
		}
		export_excel('订单列表', $excel);
	}
}